package com.slw.emergency.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.slw.emergency.entity.Epidemic;
import com.slw.emergency.entity.OverseaEpidemic;
import com.slw.emergency.enums.StatusEnum;
import com.slw.emergency.mapper.EpidemicMapper;
import com.slw.emergency.model.*;
import com.slw.emergency.service.EpidemicService;
import com.slw.emergency.service.OverseaEpidemicService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class EpidemicServiceImpl extends ServiceImpl<EpidemicMapper, Epidemic> implements EpidemicService {

    @Autowired
    private OverseaEpidemicService overseaEpidemicService;

    @Override
    public RespChinaEpidemic getChinaSum(String date) {
        RespChinaEpidemic respChinaEpidemic = new RespChinaEpidemic();
        String currentDate = date;
        //获取给定日期的确诊总数、疑似总数、治愈总数、死亡总数
        Map<String, Long> todayResult = this.baseMapper.getChinaTotal(date);
        if(null== todayResult) {
            //如果今天的数据为空，则使用昨天的数据作为今天的数据
            String yestDate = getSpecifiedDayBefore(date);
            currentDate = yestDate;
            todayResult = this.baseMapper.getChinaTotal(yestDate);
        }
        respChinaEpidemic.setCntSumConfirmed(String.valueOf(todayResult.get("confirmed")));
        respChinaEpidemic.setCntSumSuspected(String.valueOf(todayResult.get("suspected")));
        respChinaEpidemic.setCntSumCured(String.valueOf(todayResult.get("cured")));
        respChinaEpidemic.setCntSumDead(String.valueOf(todayResult.get("dead")));
        respChinaEpidemic.setCntIncConfirmed(String.valueOf(todayResult.get("cntconfirm")));
        respChinaEpidemic.setCntIncSuspected(String.valueOf(todayResult.get("cntsuspect")));
        respChinaEpidemic.setCntIncCured(String.valueOf(todayResult.get("cntheal")));
        respChinaEpidemic.setCntIncDead(String.valueOf(todayResult.get("cntdead")));

        respChinaEpidemic.setUpdateTime(getDateString(currentDate));
        return respChinaEpidemic;
    }

    @Override
    public List<String> getDateList() {
        return this.baseMapper.getDateList();
    }

    @Override
    public RespStatisticsEpidemic getStatisticsEpidemic() {
        List<String> dateList = getDateList();
        RespStatisticsEpidemic respStatisticsEpidemic = new RespStatisticsEpidemic();
        respStatisticsEpidemic.setDateList(dateList);

        List<String> confirmList = new ArrayList<>();
        List<String> suspectedList = new ArrayList<>();
        List<String> curedList = new ArrayList<>();
        List<String> deadList = new ArrayList<>();
        List<String> confirmAddList = new ArrayList<>();
        List<String> suspectedAddList = new ArrayList<>();
        List<String> curedAddList = new ArrayList<>();
        List<String> deadAddList = new ArrayList<>();

        for(int i=0; i < dateList.size(); i++){
            RespChinaEpidemic dayResult = getChinaSum(dateList.get(i));
            confirmList.add(String.valueOf(dayResult.getCntSumConfirmed()));
            suspectedList.add(String.valueOf(dayResult.getCntSumSuspected()));
            curedList.add(String.valueOf(dayResult.getCntSumCured()));
            deadList.add(String.valueOf(dayResult.getCntSumDead()));
            confirmAddList.add(String.valueOf(dayResult.getCntIncConfirmed()));
            suspectedAddList.add(String.valueOf(dayResult.getCntIncSuspected()));
            curedAddList.add(String.valueOf(dayResult.getCntIncCured()));
            deadAddList.add(String.valueOf(dayResult.getCntIncDead()));
        }
        respStatisticsEpidemic.setDateList(dateList);
        respStatisticsEpidemic.setConfirmList(confirmList);
        respStatisticsEpidemic.setSuspectedList(suspectedList);
        respStatisticsEpidemic.setCuredList(curedList);
        respStatisticsEpidemic.setDeadList(deadList);
        respStatisticsEpidemic.setConfirmAddList(confirmAddList);
        respStatisticsEpidemic.setSuspectedAddList(suspectedAddList);
        respStatisticsEpidemic.setCuredAddList(curedAddList);
        respStatisticsEpidemic.setDeadAddList(deadAddList);

        //设置获取数据日期
        respStatisticsEpidemic.setUpdateTime(getDateString(dateList.get(dateList.size()-1)));
        return respStatisticsEpidemic;
    }

    @Override
    public List<RespProvinceEpidemic> getChinaProvince(String date) {
        //获得当天的各省的数据
        String nowDate = date;
        String yestDate = getSpecifiedDayBefore(date);
        List<RespProvinceEpidemic> todayResult = this.baseMapper.getChinaProvince(date);
        List<RespProvinceEpidemic> yestodayResult = this.baseMapper.getChinaProvince(yestDate);

        if(todayResult.size()==0){
            todayResult = yestodayResult;
            nowDate=yestDate;
        }

        for(int i=0;i<todayResult.size();i++){
            RespProvinceEpidemic province = todayResult.get(i);
            String provinceName = province.getProvince();
            //获得当天的某个省下各个市的数据
            List<ResChinaCity> cities = this.baseMapper.getChinaCity(provinceName, nowDate);
            String updateTime = getDateString(nowDate);
            cities.forEach(city -> city.setUpdateTime(updateTime));
            cities = cities.stream().sorted(Comparator.comparingLong(ResChinaCity::getCntSumConfirmed2).reversed()).collect(Collectors.toList());
            province.setCities(cities);

            //设置获取数据日期
            province.setUpdateTime(getDateString(nowDate));
        }

        //按确诊人数降序排列
        todayResult = todayResult.stream().sorted(
                Comparator.comparingLong(RespProvinceEpidemic::getCntSumConfirmed2).reversed()).collect(Collectors.toList());
        return todayResult;
    }

    @Override
    public ResChinaCity getOneCity(String city, String date) {
         ResChinaCity resChinaCity = this.baseMapper.getOneCity(city,date);
         String nowDate =date;
         if(null == resChinaCity){
             String yestDate = getSpecifiedDayBefore(date);
             resChinaCity = this.baseMapper.getOneCity(city, yestDate);
            nowDate = yestDate;
         }

        //设置获取数据日期
        resChinaCity.setUpdateTime(getDateString(nowDate));
        return resChinaCity;
    }

    /**
     * 求给定日期的前一天
     * @param specifiedDay
     * @return
     */
    public String getSpecifiedDayBefore(String specifiedDay){
        Calendar c = Calendar.getInstance();
        Date date=null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day=c.get(Calendar.DATE);
        c.set(Calendar.DATE,day-1);
        String dayBefore=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
        return dayBefore;
    }


    public Date stringToDate(String date){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return df.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getDateString(String date){
        SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
        Date res = new Date();
        try {
            res = df1.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
        return df2.format(res);
    }
}
